package com.ltsh.fim.common.model;


import com.ltsh.fim.common.inf.ResultCallback;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class FimFuture implements Future<Object> {
//    private CountDownLatch latch = new CountDownLatch(1);

    private FimRequest fimRequest;

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    @Override
    public boolean isCancelled() {
        return false;
    }

    @Override
    public boolean isDone() {
        return false;
    }

    @Override
    public Object get() throws InterruptedException {
         //boolean b = latch.await(100, TimeUnit.MICROSECONDS);
//        latch.await();
        try {
            return fimRequest.getData();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "Error";
    }

    @Override
    public Object get(long timeout, TimeUnit unit) throws InterruptedException {
//        boolean b = latch.await(timeout,unit);
        return fimRequest.getData();
    }
    private ResultCallback resultCallback;
    public void setResultCallback(ResultCallback resultCallback) {
        this.resultCallback =resultCallback;
    }
    public void done(FimRequest fimRequest){
        this.fimRequest = fimRequest;
        if(resultCallback != null) {

            resultCallback.callback(fimRequest.getData());
        }
//        latch.countDown();
    }
}
